package com.lab.system.service.impl;

import com.lab.common.core.domain.entity.SysUser;
import com.lab.common.enums.EquipmentBorrowStatus;
import com.lab.common.enums.LabStatus;
import com.lab.system.domain.Equipment;
import com.lab.system.domain.EquipmentBorrow;
import com.lab.system.mapper.EquipmentBorrowMapper;
import com.lab.system.service.IEquipmentBorrowService;
import com.lab.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 实验设备借用Service业务层处理
 *
 * @author cf
 * @date 2025-02-15
 */
@Service
public class EquipmentBorrowServiceImpl implements IEquipmentBorrowService {
    @Autowired
    private EquipmentBorrowMapper equipmentBorrowMapper;

    @Autowired
    private EquipmentServiceImpl equipmentService;

    @Autowired
    private ISysUserService userService;


    /**
     * 查询实验设备借用
     *
     * @param id 实验设备借用主键
     * @return 实验设备借用
     */
    @Override
    public EquipmentBorrow selectEquipmentBorrowById(Long id) {
        EquipmentBorrow equipmentBorrow = equipmentBorrowMapper.selectEquipmentBorrowById(id);
        Equipment equipment = equipmentService.selectEquipmentById(equipmentBorrow.getEquipmentId());
        if (equipment != null) {
            equipmentBorrow.setEquipmentName(equipment.getEquipmentName());
        }
        SysUser sysUser = userService.selectUserById(equipmentBorrow.getUserId());
        if (sysUser != null) {
            equipmentBorrow.setUserName(sysUser.getUserName());
        }
        return equipmentBorrow;
    }

    /**
     * 查询实验设备借用列表
     *
     * @param equipmentBorrow 实验设备借用
     * @return 实验设备借用
     */
    @Override
    public List<EquipmentBorrow> selectEquipmentBorrowList(EquipmentBorrow equipmentBorrow) {
        List<EquipmentBorrow> equipmentBorrows = equipmentBorrowMapper.selectEquipmentBorrowList(equipmentBorrow);
        for (EquipmentBorrow equipmentBorrow1 : equipmentBorrows) {
            Equipment equipment = equipmentService.selectEquipmentById(equipmentBorrow1.getEquipmentId());
            if (equipment != null) {
                equipmentBorrow1.setEquipmentName(equipment.getEquipmentName());
            }
            SysUser sysUser = userService.selectUserById(equipmentBorrow1.getUserId());
            if (sysUser != null) {
                equipmentBorrow1.setUserName(sysUser.getUserName());
            }
            equipmentBorrow1.setStatusDesc(EquipmentBorrowStatus.getLabStatus(equipmentBorrow1.getStatus()).getInfo());
        }
        return equipmentBorrows;
    }

    /**
     * 新增实验设备借用
     *
     * @param equipmentBorrow 实验设备借用
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEquipmentBorrow(EquipmentBorrow equipmentBorrow) {
        equipmentBorrow.setStatus(EquipmentBorrowStatus.USING.getCode());

        Equipment equipment = equipmentService.selectEquipmentById(equipmentBorrow.getEquipmentId());
        if (equipment == null) {
            throw new RuntimeException("设备不存在");
        }

        equipment.setStatus(LabStatus.RENTING.getCode());

        equipmentService.updateEquipment(equipment);

        return equipmentBorrowMapper.insertEquipmentBorrow(equipmentBorrow);
    }

    /**
     * 修改实验设备借用
     *
     * @param equipmentBorrow 实验设备借用
     * @return 结果
     */
    @Override
    public int updateEquipmentBorrow(EquipmentBorrow equipmentBorrow) {
        return equipmentBorrowMapper.updateEquipmentBorrow(equipmentBorrow);
    }

    /**
     * 批量删除实验设备借用
     *
     * @param ids 需要删除的实验设备借用主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentBorrowByIds(Long[] ids) {
        return equipmentBorrowMapper.deleteEquipmentBorrowByIds(ids);
    }

    /**
     * 删除实验设备借用信息
     *
     * @param id 实验设备借用主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentBorrowById(Long id) {
        return equipmentBorrowMapper.deleteEquipmentBorrowById(id);
    }
}
